package com.dcivision.dms.web;

/*
 * @(#)MaintDmsDefaultProfileSettingAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */


import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.DmsOperationConstant;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.dao.DmsDefaultProfileSettingDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.SysParameter;
import com.dcivision.framework.dao.SysParameterDAObject;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractMaintAction;



/**
  MaintDmsDefaultProfileSettingAction.java

  This class is for maint default profile setting.

    @author          Tony Chen
    @company         DCIVision Limited
    @creation date   01/12/2003
    @version         $Revision: 1.18.2.3 $
*/

public class MaintDmsDefaultProfileSettingAction extends AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.18.2.3 $";

  public MaintDmsDefaultProfileSettingAction() {
    super();
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.dms.dao.DmsDefaultProfileSettingDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_DOC_PROFILE);
  }

  /**
   * setPageTitle
   * set the extend page title and page path.
   * default page path/title will be created by navmode and functionCode
   * @throws ApplicationException 
   */
  public void setPageTitle(HttpServletRequest request, HttpServletResponse response, ActionForm form, ActionMapping mapping, ActionForward actionForward){
    String extendTitle = MessageResourcesFactory.getMessage(this.getSessionContainer(request).getSessionLocale(), "dms.label.default_profile");
    request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE, extendTitle);
    request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE_SHOW_ONLY, new Boolean(true));
  }
  
  public ActionForward execute(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response) throws
    ServletException {


    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
    if (forward!=null) {
      return forward;
    }
    String opMode = request.getParameter("opMode");
    String navMode = request.getParameter("navMode");
    if ( (opMode == null) && (navMode == null)) {
      this.selectForEditRecord(mapping, form, request, response);
      return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
    } else if ( DmsOperationConstant.SAVE_DEF_PROFILE_LIST.equals(opMode) ) {
      this.saveDefProfileDisplayOption(mapping, form, request, response);
      this.commit(request);
      this.selectForEditRecord(mapping, form, request, response);     
      addMessage(request, MESSAGE_RECORD_INSERT);
      return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
    }else if(GlobalConstant.NAV_MODE_REFRESH.equals(opMode)){    	
      this.selectForEditRecord(mapping, form, request, response);
      return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
    }else if("GET_FIELD_NUMBER".equals(opMode)){
      Integer olderNumber = SystemParameterFactory.getSystemParameterInteger(SystemParameterConstant.DMS_DEFAULT_PROFILE_FIELD_COUNT);
      this.getFieldNumber(mapping, form, request, response);
      this.commit(request);
      Integer newNumber = SystemParameterFactory.getSystemParameterInteger(SystemParameterConstant.DMS_DEFAULT_PROFILE_FIELD_COUNT);
      this.selectForEditRecord(mapping, form, request, response); 
      if(!Utility.isEmpty(newNumber)&&!Utility.isEmpty(olderNumber)&&newNumber.intValue()!=olderNumber.intValue()) {
        if(newNumber.intValue()>olderNumber.intValue()) {
          addMessage(request, MESSAGE_RECORD_INSERT);
        } else {
          addMessage(request, MESSAGE_RECORD_DELETE);
        }
      }
      return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
    }else {
      try { request.getSession().getServletContext().removeAttribute(com.dcivision.dms.DmsOperationConstant.DMS_DEFAULT_PROFILE_SETTING); } catch (Exception ignore) {}
      // Retrieve the default document profile settings.
      SessionContainer sessionContainer = this.getSessionContainer(request);
      Connection conn = this.getConnection(request);
      DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
      docRetrievalManager.checkNSetDefaultProfileList(request);      
      docRetrievalManager.release();
      return super.execute(mapping, form, request, response);
      
    }
  }



  /**
   * Select for the save the default profile showing in listing page
   * @param mapping          ActionMapping
   * @param form             ActionForm
   * @param request          HttpServletRequest
   * @param response         HttpServletResponse
   * @throws                 ApplicationException
   */
  public void saveDefProfileDisplayOption(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws ApplicationException {

    SessionContainer sessionCon = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    
    MaintDmsDefaultProfileSettingForm profileForm = (MaintDmsDefaultProfileSettingForm)form;
    String showDefaultProfile = "0";
    String showBasicSelectedID = "0";
    String showBasicSelected = "0";
    String showSelectedOrder = DmsOperationConstant.DEFAULT_PROFILE_SETTING_ASC;
  
 
   if((profileForm.getBasicSelectedID()!=null)&&(profileForm.getBasicSelectedID().length>0)){
     showBasicSelectedID = TextUtility.join(",", profileForm.getBasicSelectedID()); 
    }
   
   if((profileForm.getBasicSelected()!=null)&&(profileForm.getBasicSelected().length>0)){
   	 showBasicSelected = TextUtility.join(",", profileForm.getBasicSelected());
   }    
   
   if (DmsOperationConstant.DEFAULT_PROFILE_SETTING_DESC.equals(profileForm.getSelectedOrder())) {
   	 showSelectedOrder = DmsOperationConstant.DEFAULT_PROFILE_SETTING_DESC;
   }
   
   showDefaultProfile = showBasicSelectedID+"|"+showBasicSelected+"|"+showSelectedOrder;
    
    SysParameterDAObject sysParameterDAO = new SysParameterDAObject(sessionCon, conn);
    SysParameter sysParameter = (SysParameter) sysParameterDAO.getByParameterCode(SystemParameterConstant.DMS_DEFAULT_PROFILE_FIELD_LIST);
    if (sysParameter!=null) {
      sysParameter.setParameterValue(showDefaultProfile);
      sysParameterDAO.updateObject(sysParameter);
      com.dcivision.framework.SystemParameterFactory.setSystemParameter(com.dcivision.framework.SystemParameterConstant.DMS_DEFAULT_PROFILE_FIELD_LIST, showDefaultProfile);
    }  

    profileForm.setNavMode(null);
    profileForm.setOpMode(null);
    sessionCon=null;
  }

  /**
   * Select for the edit record
   * @param mapping          ActionMapping
   * @param form             ActionForm
   * @param request          HttpServletRequest
   * @param response         HttpServletResponse
   * @throws                 ApplicationException
   */
  public void selectForEditRecord(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws ApplicationException {

    SessionContainer sessionCon = this.getSessionContainer(request);
    MaintDmsDefaultProfileSettingForm profileForm = (MaintDmsDefaultProfileSettingForm)form;
    // get all record.
    DmsDefaultProfileSettingDAObject dmsDefaultProfileSettingDAO =
        (DmsDefaultProfileSettingDAObject)super.getMajorDAObject(request);
    List dmsDefaultProfileSettingList = new ArrayList();

    dmsDefaultProfileSettingList = dmsDefaultProfileSettingDAO.getFullList();
    request.setAttribute("Dms_Default_Profile_Setting_List",dmsDefaultProfileSettingList);
    
    String showDefaultProfile = com.dcivision.framework.SystemParameterFactory.getSystemParameter(com.dcivision.framework.SystemParameterConstant.DMS_DEFAULT_PROFILE_FIELD_LIST);
    if(!Utility.isEmpty(showDefaultProfile)){
      String[] checkCustomFieldArray = TextUtility.splitString(showDefaultProfile, "|");
      if(checkCustomFieldArray.length != 3 ){
        showDefaultProfile += "||"+DmsOperationConstant.DEFAULT_PROFILE_SETTING_ASC;
      }
    }else{
      showDefaultProfile ="||"+DmsOperationConstant.DEFAULT_PROFILE_SETTING_ASC;
    }    
    
    String[] customFieldArray = TextUtility.splitString(showDefaultProfile, "|");
    
    String[] selectDefaultProfile = TextUtility.splitString(customFieldArray[0], ",");
    profileForm.setBasicSelectedID(selectDefaultProfile);
    
    String[] basicSelected = TextUtility.splitString(customFieldArray[1], ",");
    profileForm.setBasicSelected(basicSelected);
    
    profileForm.setSelectedOrder(customFieldArray[2]);

    dmsDefaultProfileSettingDAO=null;
    sessionCon=null;
  }
  
  public void updateRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    super.updateRecord(mapping,   form,  request,  response);
    // Retrieve the default document profile settings.
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    try {request.getSession().getServletContext().removeAttribute(com.dcivision.dms.DmsOperationConstant.DMS_DEFAULT_PROFILE_SETTING); } catch (Exception ignore) {}
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    docRetrievalManager.checkNSetDefaultProfileList(request);      
    docRetrievalManager.release();
    
    
  }
  
  public void deleteListData(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
  	SessionContainer sessionCon = this.getSessionContainer(request);
  	Connection conn = this.getConnection(request);
    MaintDmsDefaultProfileSettingForm profileForm = (MaintDmsDefaultProfileSettingForm)form;
    DmsDefaultProfileSettingDAObject dmsDefaultProfileSettingDAO =
        (DmsDefaultProfileSettingDAObject)super.getMajorDAObject(request);
    String[] selectedID = profileForm.getBasicSelectedID();
    for(int i=0;i<selectedID.length;i++){
    	if(!Utility.isEmpty(selectedID[i])){
    		dmsDefaultProfileSettingDAO.deleteObjectByID(TextUtility.parseIntegerObj(selectedID[i]));
    		this.commit(request);
    	}
    }
    SysParameterDAObject sysParameterDAO = new SysParameterDAObject(sessionCon, conn);
    SysParameter sysParameter = (SysParameter) sysParameterDAO.getByParameterCode(SystemParameterConstant.DMS_DEFAULT_PROFILE_FIELD_COUNT);
    if(sysParameter!=null && selectedID.length>0){
    	int parameterValue=TextUtility.parseInteger(sysParameter.getParameterValue())-selectedID.length;
    	sysParameter.setParameterValue(TextUtility.formatInteger(parameterValue));
    	sysParameterDAO.updateObject(sysParameter);
        com.dcivision.framework.SystemParameterFactory.setSystemParameter(com.dcivision.framework.SystemParameterConstant.DMS_DEFAULT_PROFILE_FIELD_COUNT, TextUtility.formatInteger(parameterValue));
    }
    List dmsDefaultProfileSettingList = new ArrayList();
    dmsDefaultProfileSettingList = dmsDefaultProfileSettingDAO.getFullList();
    request.setAttribute("Dms_Default_Profile_Setting_List",dmsDefaultProfileSettingList);
    
    dmsDefaultProfileSettingDAO=null;
    sessionCon=null;
    }
  
  public void getFieldNumber(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
  	SessionContainer sessionCon = this.getSessionContainer(request);
  	Connection conn = this.getConnection(request);
  	
    //String showBasicSelectedID = "0";
    //String showBasicSelected = "0";
    //String showSelectedOrder = DmsOperationConstant.DEFAULT_PROFILE_SETTING_ASC;
    String showDefaultProfile = com.dcivision.framework.SystemParameterFactory.getSystemParameter(com.dcivision.framework.SystemParameterConstant.DMS_DEFAULT_PROFILE_FIELD_LIST);
    
    MaintDmsDefaultProfileSettingForm profileForm = (MaintDmsDefaultProfileSettingForm)form;
    DmsDefaultProfileSettingDAObject dmsDefaultProfileSettingDAO = (DmsDefaultProfileSettingDAObject)super.getMajorDAObject(request);
    SysParameterDAObject sysParameterDAO = new SysParameterDAObject(sessionCon, conn);
    SysParameter sysParameter = (SysParameter) sysParameterDAO.getByParameterCode(SystemParameterConstant.DMS_DEFAULT_PROFILE_FIELD_COUNT);
        
    int parameterValue=TextUtility.parseInteger(profileForm.getNumber());
    Integer tmpNumber = SystemParameterFactory.getSystemParameterInteger(SystemParameterConstant.DMS_DEFAULT_PROFILE_FIELD_COUNT);
    
    if(sysParameter!=null){
//      int parameterValue=TextUtility.parseInteger(profileForm.getNumber());
      sysParameter.setParameterValue(TextUtility.formatInteger(parameterValue));    	
      sysParameterDAO.updateObject(sysParameter);
      com.dcivision.framework.SystemParameterFactory.setSystemParameter(com.dcivision.framework.SystemParameterConstant.DMS_DEFAULT_PROFILE_FIELD_COUNT, TextUtility.formatInteger(parameterValue));
    }
    
//    if(!Utility.isEmpty(showDefaultProfile)){
//      String[] checkCustomFieldArray = (String[]) TextUtility.splitString(showDefaultProfile, "|");
//      if(checkCustomFieldArray.length != 3 ){
//   	    showDefaultProfile ="||"+DmsOperationConstant.DEFAULT_PROFILE_SETTING_ASC;
//      }else{
//      	showDefaultProfile = "|"+checkCustomFieldArray[1]+"|"+checkCustomFieldArray[2];
//      }
//    }else{
//      showDefaultProfile ="||"+DmsOperationConstant.DEFAULT_PROFILE_SETTING_ASC;
//    }
    
    if(!Utility.isEmpty(showDefaultProfile)){
    	String[] customFieldArray = TextUtility.splitString(showDefaultProfile, "|");
      String[] selectDefaultProfile = TextUtility.splitString(customFieldArray[0], ",");
      
      if (parameterValue < tmpNumber.intValue()){
      	customFieldArray[0] = "0";
      	
      	StringBuffer tmpDefaultProfile = new StringBuffer();
        for (int i=0;i<customFieldArray.length;i++){
        	tmpDefaultProfile.append(customFieldArray[i]);
        	if (i<customFieldArray.length -1){
        		tmpDefaultProfile.append("|");
        	}
        }
        showDefaultProfile = tmpDefaultProfile.toString();
        
      }
    }
    
    SysParameter sysParameter2 = (SysParameter) sysParameterDAO.getByParameterCode(SystemParameterConstant.DMS_DEFAULT_PROFILE_FIELD_LIST);
    if (sysParameter2!=null) {
      sysParameter2.setParameterValue(showDefaultProfile);
      sysParameterDAO.updateObject(sysParameter2);
      com.dcivision.framework.SystemParameterFactory.setSystemParameter(com.dcivision.framework.SystemParameterConstant.DMS_DEFAULT_PROFILE_FIELD_LIST, showDefaultProfile);
    }
    
    List dmsDefaultProfileSettingList = new ArrayList();
    dmsDefaultProfileSettingList = dmsDefaultProfileSettingDAO.getFullList();
    request.setAttribute("Dms_Default_Profile_Setting_List",dmsDefaultProfileSettingList);
    
    dmsDefaultProfileSettingDAO=null;
    sessionCon=null;
  }
}


